import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/models/odoo/purchase_order.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';
import '../../../generated/l10n.dart';

class PurchaseOrderDialog extends StatefulWidget {
  PurchaseOrderDialog();

  @override
  _PurchaseOrderDialogState createState() => _PurchaseOrderDialogState();
}

class _PurchaseOrderDialogState extends State<PurchaseOrderDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  PurchaseOrder purchaseOrder = PurchaseOrder();
  DateFormat format= DateFormat('yyyy-MM-dd');
  @override
  void initState() {
    super.initState();
    purchaseOrder.dateOrderStart = Utils.getDate();
    purchaseOrder.dateOrderEnd = Utils.getDate();
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelectDate(
            context,
            label: '接收开始日期',
            value: purchaseOrder.dateOrderStart,
            lastDate: isNotBlank(purchaseOrder.dateOrderEnd)?format.parse(purchaseOrder.dateOrderEnd!):null,
            onDateChange: (v) {
              formKey.currentState?.save();
              purchaseOrder.dateOrderStart = v;
              setState(() {});
            },
            onSaved: (v) {
              purchaseOrder.dateOrderStart = v;
            },
          ),
          CrySelectDate(
            context,
            label: '接收结束日期',
            value: purchaseOrder.dateOrderEnd,
            firstDate: isNotBlank(purchaseOrder.dateOrderStart)?format.parse(purchaseOrder.dateOrderStart!):null,
            onDateChange: (v) {
              formKey.currentState?.save();
              purchaseOrder.dateOrderEnd = v;
              setState(() {});
            },
            onSaved: (v) {
              purchaseOrder.dateOrderEnd = v;
            },
          ),
          CryInput(
            label: '采购订单编号',
            value: purchaseOrder.name,
            onSaved: (v) {
              purchaseOrder.name = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, purchaseOrder);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
